package com.zhenyi.tuangou.index;

import java.util.List;

import org.eclipse.jetty.server.Authentication.User;

import com.cjw.model.DBMap;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.zhenyi.common.model.TgGroup;
import com.zhenyi.common.model.TgGroupMember;
import com.zhenyi.common.model.TgGroupProduct;
import com.zhenyi.common.model.TgProduct;
import com.zhenyi.common.model.TgUser;
import com.zhenyi.tuangou.interceptor.WXInterceptor;

public class GroupProductController extends TGController {

	/**
	 * 我的参团
	 */
	@Clear
	public void index() {
		TgUser user = getUser();
		Kv cond = Kv.by("userId", user.getId());
		SqlPara sqlPara = Db.getSqlPara("findGroupBuUserId", cond);
		List<Record> list = Db.find(sqlPara);
		renderJson(list);
	}

	/**
	 * 团购列表
	 */
	public void groupProduct() {
		// int pageNumber = getPage();
		Long productId = getParaToLong("id");
		if (productId == null) {
			renderFailure();
			return;
		}
		TgProduct product = getProduc(productId);// 商品信息
		setAttr("product", product);
		// 获取团购列表
		List<Record> groupBuys = getGroup(productId);
		setAttr("list", groupBuys);
		render("/groupProductList.html");
		System.out.println(product.toString());
		System.out.println(groupBuys.toString());
		renderJson(groupBuys);
	}

	// 团购详情
	@Before(WXInterceptor.class)
	public void findGroupProduct() {
		String code = getPara(0);
		Long groupId = hashids.decode(code)[0];
		// 获取团购详情
		List<Record> list = getGroupById(groupId);// 参团的信息
		Long ID = list.get(0).getLong("groupProductId");
		TgGroupProduct tgGroupProduct = TgGroupProduct.dao.findById(ID);
		TgProduct product = getProduc(tgGroupProduct.getProductId());// 商品详情
		int status = TgGroup.STATUS_IN;
		if (tgGroupProduct.getNum() == list.size()) {
			status = TgGroup.STATUS_FINISH;
		}
		Kv cond = Kv.by("groupId", groupId);
		SqlPara sql = Db.getSqlPara("findGroupBuyById", cond);
		Record groupBuy = Db.find(sql).get(0);

		DBMap map = new DBMap();
		map.put("groupId", groupId);
		map.put("userId", getUser().getId());
		TgGroupMember groupMember = TgGroupMember.dao.searchFirst(map);
		setAttr("isJoined", groupMember != null);
		setAttr("status", status);
		setAttr("group", groupBuy);
		setAttr("product", product);
		setAttr("list", list);
		setAttr("shareCode", hashids.encode(groupId));
		render("/res/groupBuyingInfo.html");
	}

	/**
	 * 获取团购中人员未满的团购列表
	 * 
	 * @param pageNumber
	 * @param productId
	 * @return
	 */
	protected List<Record> getGroup(long productId) {
		Kv cond = Kv.by("productId", productId);
		SqlPara sql = Db.getSqlPara("findGroupBuy", cond);
		// Page<Record> groupBuys = Db.paginate(pageNumber, PAGE_SIZE, sql);
		List<Record> groupBuys = Db.find(sql);
		return groupBuys;
	}

	/**
	 * 获取团购详情
	 * 
	 * @param groupId
	 * @return
	 */
	protected List<Record> getGroupById(long groupId) {
		Kv cond = Kv.by("groupId", groupId);
		SqlPara sql = Db.getSqlPara("findGroupMember", cond);
		List<Record> list = Db.find(sql);// 参团的信息
		return list;
	}

	/**
	 * 我的团购
	 */
	public void myGroup() {
		SqlPara sqlPara = Db.getSqlPara("FindMyGroup", getUser().getId());
		List<Record> items = Db.find(sqlPara);
		for (Record record : items) {
			Long groupId = record.getLong("groupId");
			String shareCode = hashids.encode(groupId);
			record.set("shareCode", shareCode);
			record.set("code", hashids.encode(record.getLong("orderId")));
		}
		setItems(items);
		render("/res/groupList.html");
	}
}
